package com.hooker.adapter

import android.content.Context
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hooker.bean.PackageInfo
import com.hooker.bean.TypeInfo
import com.hooker.util.IO
import com.sdk.hooker.penetration.R
import org.json.JSONArray

class SimpleAdapter : RecyclerView.Adapter<SimpleAdapter.PackageHolder> {

    var context: Context? = null
    var list: MutableList<PackageInfo> = mutableListOf()
    var listView: RecyclerView? = null
    lateinit var mClickListener: OnItemClickListener
    var currentPosition: Int = -1
    var typeNames: MutableList<TypeInfo> = mutableListOf()

    constructor(context: Context, list: MutableList<PackageInfo>, view: RecyclerView) {
        this.context = context
        this.list.addAll(list)
        this.listView = view

        getTypeData()
    }

    private fun getTypeData() {
        typeNames.clear()
        var typeList = IO.getFromAssets(context, "type.json")
        var array = JSONArray(typeList)
        for (index in 0 until array.length()) {
            var json = array.getJSONObject(index)
            var id = json.optString("id")
            var name = json.optString("name")
            var type = json.optString("type", "0")
            typeNames.add(TypeInfo(id, name, type))
        }
    }

    fun update(data: MutableList<PackageInfo>) {
        this.list.clear()
        this.list.addAll(data)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.mClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PackageHolder {
        var view: View = View.inflate(context, R.layout.layout_package, null)
        view.layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )
        return PackageHolder(view)
    }


    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: PackageHolder, position: Int) {
        var info = list?.get(position)

        holder.name?.text = info?.name

        holder.icon?.setImageDrawable(info?.icon)

        holder.packageView.setTag(R.id.position, position)
        holder.packageView.tag = info

        holder.packageView.setOnClickListener { view ->
            var pos = view.getTag(R.id.position) as Int

            currentPosition = when (currentPosition) {
                pos -> -1
                else -> pos
            }

            mClickListener?.onClick(view, pos)
        }

        if (currentPosition == position) {
            holder.typeList.visibility = View.VISIBLE
        } else {
            holder.typeList.visibility = View.GONE
        }

        var adapter = TypeListAdapter(context, typeNames, holder.typeList, info.name)
        holder.typeList.adapter = adapter

        holder.typeList.overScrollMode = View.OVER_SCROLL_NEVER
        handleScroll(holder)
    }

    private fun handleScroll(holder: PackageHolder) {
        holder.typeList?.addOnItemTouchListener(object : RecyclerView.OnItemTouchListener {
            override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {
            }

            override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
            }

            override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
                if (e != null) {
                    // 找到被点击位置的item的rootView
                    var itemView: View? = rv.findChildViewUnder(e.x, e.y)

                    if (itemView != null) {
                        (itemView.parent as ViewGroup).requestDisallowInterceptTouchEvent(true)
                    }
                }
                return false
            }
        })
    }


    class PackageHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var icon: ImageView? = null
        var name: TextView? = null
        var typeList: RecyclerView
        var packageView: LinearLayout

        init {
            icon = itemView.findViewById(R.id.icon)
            name = itemView.findViewById(R.id.name)
            packageView = itemView.findViewById(R.id.layout_package)
            typeList = itemView.findViewById(R.id.type_list)
            typeList.layoutManager =
                LinearLayoutManager(itemView.context, RecyclerView.VERTICAL, false)
        }
    }

    interface OnItemClickListener {
        fun onClick(view: View, position: Int)
    }
}